﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using CubaLibreBar.Model.Core.Entities;

namespace CubaLibreBar.Model.Dados
{
    public static class UsuariosDAO
    {
        public static bool SenhaCorreta(string nomeUsuario, string senha)
        {
            Usuarios usuario = new Usuarios();
            
            using (SqlCeConnection connection = ConnectionFactory.GetConnection())
            {
                connection.Open();

                using (SqlCeCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"SELECT * FROM tbUsuarios WHERE NomeUsuario = @Usuario";
                    cmd.Parameters.Add("@Usuario", nomeUsuario);

                    using (SqlCeDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            usuario.NomeUsuario = reader["NomeUsuario"].ToString();
                            usuario.Senha = reader["Senha"].ToString();
                        }
                    }
                }

                connection.Close();
            }

            if (usuario.Senha != null)
            {
                if (usuario.Senha.Equals(senha))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public static void AtualizaSenha(string nomeUsuario, string novaSenha)
        {
            using (SqlCeConnection connection = ConnectionFactory.GetConnection())
            {
                connection.Open();

                using (SqlCeCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"UPDATE tbUsuarios SET Senha = @Senha WHERE NomeUsuario = @Usuario";
                    cmd.Parameters.Add("@Usuario", nomeUsuario);
                    cmd.Parameters.Add("@Senha", novaSenha);

                    cmd.ExecuteNonQuery();
                }

                connection.Close();
            }
        }
    }
}
